//
//  CXViewController.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/18.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit

class CXMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        //替换掉原有的tabBar 利用KVC修改只读属性
        let tabbar = CXTabBar()
        //block里存在循环引用
        tabbar.composedClick = { [weak self] in
            print("点我啦")
            //将自定义view添加上来 附带按钮动画
            if self != nil {
                let composeV = CXComposeView()
                composeV.getParentController(self!)
            }
            //UIApplication.sharedApplication().delegate?.window?!.addSubview(customView)
            //last是最上层
//            UIApplication.sharedApplication().windows.last?.addSubview(customView)
//            UIApplication.sharedApplication().keyWindow?.addSubview(customView)
            
        }
        //KVC 修改系统属性
        setValue(tabbar, forKeyPath: "tabBar")
        //添加子控制器
        addChildViewControllers()
    }
    //MARK: 添加子控制器集
    func addChildViewControllers() {
        addChildViewController(CXDiscoverController(), title: "发现", imageNamed: "tabbar_discover")
        addChildViewController(CXHomeController(), title: "首页", imageNamed: "tabbar_home")
        addChildViewController(CXMessageController(), title: "消息", imageNamed: "tabbar_message_center")
        addChildViewController(CXProfileController(), title: "我", imageNamed: "tabbar_profile")
    }
    //MARK: 添加子控制器
    func addChildViewController(childController: UIViewController, title:String, imageNamed:String) {
        //设置每一个子控制器的tab
        childController.title = title
        let ti = CXTabBarItem()
        ti.vc = self
        childController.tabBarItem = ti
        if title == "消息" {
            childController.tabBarItem.badgeValue = "8"
        }
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        childController.tabBarItem.image = UIImage(named: imageNamed)
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageNamed)_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
            //设置选中后tabBarItem文字的颜色
        let textColor = [NSForegroundColorAttributeName:UIColor.orangeColor()]
        childController.tabBarItem.setTitleTextAttributes(textColor, forState: .Selected)
        //将子控制器加入tabBarController
        addChildViewController(CXNavigationController(rootViewController: childController))
    }
 
    

}
